package page;

import common.Object;
import common.User;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

public class LoginPage {
    EdgeDriver edgeDriver = Object.getEdgeDriver();
    private final String loginUrl = "http://82.156.186.83:8080/sign-in.html";


    public void openLoginPage() throws InterruptedException {
        edgeDriver.get(loginUrl);
        WebElement loginTitle = edgeDriver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));

        Thread.sleep(2000);
        System.out.println(loginTitle.getText());
        System.out.println("登录页面查找:" + "用户登录".equals(loginTitle.getText()));
    }

    /*
     *
     * 1.打开登录页面
     * 2.查找到去注册按钮
     * 3.调用click
     * 4.跳转到注册页面
     * 5.判断注册页面特有元素是否存在 == 》 注册页面
     */
    public void goToRegisterPage() throws InterruptedException {
        openLoginPage();
        WebElement registerButton = edgeDriver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a"));
        Thread.sleep(2000);
        registerButton.click();
        //查找注册页面元素
        WebElement registerElement = edgeDriver.findElement(By.cssSelector("#signUpForm > div > h2"));
        Thread.sleep(1000);
        System.out.println(registerElement.getText());
        System.out.println("用户注册点击："+"用户注册".equals(registerElement.getText()));
    }

    /**
     * 1.打开登录页面
     * 2.在用户名和密码上输入已注册的账号
     *    1）
     * 3.点击登录
     * 4.获取首页特殊元素
     */

    public void login (User user) throws InterruptedException {
        openLoginPage();
        WebElement userNameInput = edgeDriver.findElement(By.cssSelector("#username"));
        Thread.sleep(1000);
        userNameInput.sendKeys(user.getName());

        WebElement passwordInput = edgeDriver.findElement(By.cssSelector("#password"));
        Thread.sleep(1000);
        passwordInput.sendKeys(user.getPassword());

        WebElement loginButton = edgeDriver.findElement(By.cssSelector("#submit"));
        loginButton.click();
        Thread.sleep(2000);
        String title = edgeDriver.getTitle();
        if ("论坛 - 用户登录".equals(title)){
            System.out.println("登录失败");
        } else if ("论坛".equals(title)) {
            System.out.println("登录成功");
        }
    }


    public void close(){
        edgeDriver.close();
    }
}
